-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refresh Request.accept functionality #2687
Conversation
Codecov ReportBase: 88.603% // Head: 88.567% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #2687 +/- ##
=============================================
- Coverage 88.603% 88.567% -0.036%
=============================================
Files 87 87
Lines 6853 6849 -4
Branches 1171 1176 +5
=============================================
- Hits 6072 6066 -6
+ Misses 539 538 -1
- Partials 242 245 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Had a brief look at the code but not properly yet, commenting based on the summary you made:
OK. The original implementation had something else named Accept, so I kept the match object named differently to avoid confusion.
These are problematic. I suggest not making any of the things behave like strings to avoid possible confusion especially with non-standard comparisons
What is this needed for? This feature is already bloated, would rather keep it smaller (as stated already in #2200).
Keeping the parts separate custom types is bad for performance. I believe this was the primary reason why my PR is twice faster.
I will look at that more closer later. My implementation intentionally kept header and what they are matched against asymmetric in some cases, not pretending that MediaType (of header) and MIME str (match argument) are the same. Note that it also allows matching against a parameter but a parameter on header does not cause match to fail. |
I have run some benchmarks on the various implementations.
So changing from Testing further, going to a cached key for sorting is |
Good changes. I still suggest leaving out the comparison operators of match objects (btw, I do find the name Accept of that still a bit confusing). The intended use case with my PR is checking which argument matched: if request.accept.match("application/json", "text/html") == "text/html":
# respond HTML
else:
# respond JSON Granted, there are good alternative ways to do that e.g. by accessing the mime property, and many possible semantics for equality comparison of the match objects. Still, having multiple match objects compare by their quality is certainly stretching that and it is hard to imagine real use for that in applications, when the match function already performs selection of best format. Also, the PR still mixes comparisons by plain q vs. by the sort key, and should be consistent one way or the other in all its operations. |
OK
This is certainly a question for open debate. Consider the following change in # from ...
a = sorted(
(-acc.q, i, j, mime, acc)
for j, acc in enumerate(self)
if accept_wildcards or not acc.has_wildcard
for i, mime in enumerate(mimes)
if acc.match(mime)
)
# to ...
a = sorted(
(*acc.key, i, j, mime, acc)
for j, acc in enumerate(self)
if accept_wildcards or not acc.has_wildcard
for i, mime in enumerate(mimes)
if acc.match(mime)
) What would you expect the result of this to be? accept = parse_accept("text/*, text/plain, text/plain;format=flowed, */*")
accept.match("text/csv", "text/plain") Should it be "try and give me I think it would be more intuitive if the result was What if we allow both? This edge case certainly needs to be documented though no matter how we handle it. request.accept.match(..., rfc_priority=True) # better name?
# ... prefer_non_wildcard=True or prefer_explicit=True I honestly am not sure we need to go that far. I think for this use case sort by just q value makes sense. This is a different use case than the sorting pattern in the RFC. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we've had enough review of this. In principle all looks good to me and it certainly is an improvement over what we had. If you have any finishing touches, feel free to do them but this is LGTM as is or with changes.
Moved documentation from other PR (may not be fully up to date but archiving here anyway): Accept headerSanic has had a helper for parsing the Accept header in HTTP requests since version 21.9, PR #2200. This feature was never documented, but it provides two methods for matching: Additionally, This PR rewrites the entire handling, changing its semantics and inner function, making it behave in a more practical, less surprising way, and with additional and removed functionality. A default value of The match function can now take multiple MIME types as arguments and return the best match based on both the client's and the app's preferences. It returns a The A deprecation warning will be needed for version 22.12 LTS users who would be affected by these changes, with alternatives that can work with old and new versions. |
This falls on the heels of #2663 and #2668 and builds of the work that @Tronic did in the latter of the two PRs. This closely tracks his changes with some additions to make the pattern more compatible with the existing implementation. The following are the changes from the implementation in #2668:
Matched
>>Accept
Accept.__eq__
operator also checksq
valueAccept
match
to AcceptMediaType
parts to have wildcardMediaType
to match usingstr
orMediaType
as inputThe main breaking changes from
main
are that thein
operator is not longer equivalent tomatch
. Also, the params to use wildcards has been simplified inmatch
to a single flag.